//
//  XJComposeController.m
//  weiboOC
//
//  Created by 徐绩 on 16/5/2.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "XJComposeController.h"
#import "XJUserAccountViewModel.h"
#import "XJTextView.h"
#import "XJComposeToolbar.h"

@interface XJComposeController ()

@property (nonatomic,strong) XJComposeToolbar *composeToolbar;

@end

@implementation XJComposeController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupUI];
}


/**
 *  布局界面
 */
-(void)setupUI{
    
    [self setupNav];
    [self loadTextView];
    [self loadComposeToolbar];
    
    //键盘改变的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    
}
-(void)keyboardChangeFrame:(NSNotification *)noti{

    //Log(@"%@",noti.userInfo);
    CGRect endFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    [self.composeToolbar updateConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(endFrame.origin.y - screenH);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.view layoutIfNeeded];
    }];
                       
}
#pragma mark - 布局界面
-(XJTextView *)loadTextView{

    XJTextView *textView = [[XJTextView alloc]init];
    [self.view addSubview:textView];
    [textView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).offset(5);
    }];


    return textView;
}

-(XJComposeToolbar *)loadComposeToolbar{

    XJComposeToolbar *composeToolbar = [[XJComposeToolbar alloc]init];
    self.composeToolbar = composeToolbar;
    [self.view addSubview:composeToolbar];
    [composeToolbar makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.equalTo(44);
    }];
    
    return composeToolbar;
}


/**
 *  设置导航栏
 */
-(void)setupNav{

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(didClickBackItem)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:[self setupRightItem]];
    self.navigationItem.rightBarButtonItem.enabled = NO;
    self.navigationItem.titleView = [self setupTitleView];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor darkGrayColor];

}
/**
 *  导航条title
 */
-(UILabel *)setupTitleView{

    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.numberOfLines = 0;
    
    NSString *name = [XJUserAccountViewModel sharedViewModel].accout.name;
    
    if (name) {
        NSString * text = [NSString stringWithFormat:@"发微博\n%@",name];
        NSRange range = [text rangeOfString:name];
        NSMutableAttributedString *attText = [[NSMutableAttributedString alloc]initWithString:text];
        [attText addAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],NSFontAttributeName:[UIFont systemFontOfSize:12] }range:range];

        titleLabel.attributedText = attText;
        
    }else{
        titleLabel.text = @"发微博";
    }
    [titleLabel sizeToFit];
    return titleLabel;
}

/**
 *  导航栏右侧item
 */
-(UIButton *)setupRightItem{
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 45, 28)];
    [btn addTarget:self action:@selector(didClickSendItem) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    [btn setTitle:@"发送" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateDisabled];
    [btn setBackgroundImage:[UIImage imageNamed:@"common_button_orange"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"common_button_orange_highlighted"] forState:UIControlStateHighlighted];
    
    return btn;


}

#pragma mark - 按钮点击

/**
 *  点击返回
 */
-(void)didClickBackItem{

    [self dismissViewControllerAnimated:YES completion:nil];

}
/**
 *  点击发送
 */
-(void)didClickSendItem{

}


@end
